﻿using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using static CoreLims.Models.CoreLims.Env;

namespace CoreLims.Models.CoreLims
{
    public static class EnvExtension
    {
        public static AreaAndLocation getAreaAndLocation(this Env env, CoreLimsContext dbContext, string Id) =>
            dbContext.AreaAndLocation.FirstOrDefault(m => m.Id == Id);

        /// <summary>
        /// 获取区域列表
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dbContext"></param>
        /// <returns></returns>
        public static List<AreaAndLocation> getAreaList(this Env env, CoreLimsContext dbContext) =>
            dbContext.AreaAndLocation.Where(s => s.IsArea == true).ToList();

        /// <summary>
        /// 获取位置列表
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dbContext"></param>
        /// <returns></returns>
        public static List<AreaAndLocation> getLocationList(this Env env, CoreLimsContext dbContext) =>
            dbContext.AreaAndLocation.Where(s => s.IsArea == false).ToList();

        /// <summary>
        /// 根据父级ID，返回区域和位置列表
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dbContext"></param>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        public static List<AreaAndLocation> GetAreaAndLocationByParentId(this Env env, CoreLimsContext dbContext, string ParentId) =>
            dbContext.AreaAndLocation.Where(s => s.ParentId == ParentId)
            .OrderBy(x => x.IsArea).OrderBy(s => s.Name)
            .ToList();

        public static Monitoring getMonitoringById(this Env env, CoreLimsContext dbContext, string Id) =>
            dbContext.Monitoring.FirstOrDefault(m => m.Id == Id);

        /// <summary>
        /// 获得环境检测标准列表
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dbContext"></param>
        /// <returns></returns>
        public static List<Monitoring> getMonitoringList(this Env env, CoreLimsContext dbContext) =>
            dbContext.Monitoring.ToList();

        /// <summary>
        /// 获得顶级区域列表
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dbContext"></param>
        /// <returns></returns>
        public static List<AreaAndLocation> getAreaAndLocationRoots(this Env env, CoreLimsContext dbContext) =>
            dbContext.AreaAndLocation.Where(s => s.ParentId == null).ToList();

        /// <summary>
        /// 获取需要进行环境监测的区域和位置
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dbContext"></param>
        /// <returns></returns>
        public static List<AreaAndLocation> getEnvAreaAndLocation(this Env env, CoreLimsContext dbContext) =>
            dbContext.AreaAndLocation.Where(s => s.Interval != null && s.IntervalUnit != null).ToList();

        /// <summary>
        /// 创建一个新区域
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dbContext"></param>
        /// <param name="newAal"></param>
        /// <returns></returns>
        public static string CreateArea(this Env env, CoreLimsContext dbContext, AreaAndLocation newAal)
        {
            if (!newAal.CheckIntervalUnit)
                return "间隔单位没有设置！";

            newAal.IsArea = true;
            dbContext.AreaAndLocation.Add(newAal);
            dbContext.SaveChanges();
            return "区域创建成功！";
        }

        /// <summary>
        /// 创建一个新位置
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dbContext"></param>
        /// <param name="newAal"></param>
        /// <returns></returns>
        public static string CreateLocation(this Env env, CoreLimsContext dbContext, AreaAndLocation newAal)
        {
            if (!newAal.CheckIntervalUnit)
                return "间隔单位没有设置！";

            newAal.IsArea = false;
            dbContext.AreaAndLocation.Add(newAal);
            dbContext.SaveChanges();
            return "位置创建成功！";
        }

        /// <summary>
        /// 创建一条环境记录
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dbContext"></param>
        /// <param name="record"></param>
        /// <returns></returns>
        public static string CreateMonitoringRecord(this Env env, CoreLimsContext dbContext, MonitoringRecord other)
        {
            var aal = dbContext.AreaAndLocation.Where(s => s.Id == other.AreaAndLocationId).FirstOrDefault();
            var mon = dbContext.Monitoring.Where(s => s.Id == aal.EnvMonitoringId).FirstOrDefault();
            var record = new MonitoringRecord();
            record.Copy(other, mon);

            dbContext.MonitoringRecord.Add(record);
            dbContext.SaveChanges();
            return "环境监测记录添加成功！";
        }

        /// <summary>
        /// 获得指定区域位置的路径深度
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dbContext"></param>
        /// <param name="AreaAndLocationId"></param>
        /// <returns></returns>
        public static int getLevel(this Env env, CoreLimsContext dbContext, string AreaAndLocationId)
        {
            int a = 1;
            var aal = Common.Env.getAreaAndLocation(dbContext, AreaAndLocationId);
            while (aal.ParentId != null)
            {
                aal = Common.Env.getAreaAndLocation(dbContext, aal.ParentId);
                a ++;
            }

            return a;
        }

        /// <summary>
        /// 获得指定区域位置的路径文本
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dbContext"></param>
        /// <param name="AreaAndLocationId"></param>
        /// <returns></returns>
        public static string getDirectoryStr(this Env env, CoreLimsContext dbContext, string AreaAndLocationId)
        {
            string re = null;
            var aal = Common.Env.getAreaAndLocation(dbContext, AreaAndLocationId);
            while (aal.ParentId != null)
            { 
                re = " 》" + aal.Name + re;
                aal = Common.Env.getAreaAndLocation(dbContext, aal.ParentId);
            }
            re = aal.Name + re;

            return re;
        }

        /// <summary>
        /// 删除指定的区域位置信息
        /// </summary>
        /// <param name="env"></param>
        /// <param name="dbContext"></param>
        /// <param name="AreaAndLocationId"></param>
        /// <returns></returns>
        public static string DelAreaAndLocation(this Env env, CoreLimsContext dbContext, string AreaAndLocationId)
        {
            var count = dbContext.MonitoringRecord.Where(s => s.AreaAndLocationId == AreaAndLocationId).Count();
            if (count > 0)
                return "该区域位置已关联环境监测记录，不能删除！";

            count = dbContext.AreaAndLocation.Where(s => s.ParentId == AreaAndLocationId).Count();
            if (count > 0)
                return "该区域下存在其它区域位置信息，不能删除！";

            var aal = Common.Env.getAreaAndLocation(dbContext, AreaAndLocationId);
            dbContext.Remove(aal);
            dbContext.SaveChanges();
            return "区域位置删除成功！";
        }

        /// <summary>
        /// 区域位置关联监测标准
        /// </summary>
        /// <param name="aal"></param>
        /// <param name="dbContext"></param>
        /// <param name="EnvMonitoringId"></param>
        /// <param name="EnvMonitoringName"></param>
        /// <returns></returns>
        public static string UpdateEnv(this AreaAndLocation aal, CoreLimsContext dbContext, string EnvMonitoringId, string EnvMonitoringName)
        {
            try
            {
                aal.EnvMonitoringId = EnvMonitoringId;
                aal.EnvMonitoringName = EnvMonitoringName;
                dbContext.AreaAndLocation.Update(aal);
                dbContext.SaveChanges();
                return "监测标准更新成功！";
            }
            catch(Exception ex)
            {
                return ex.Message;
            }
        }
    }

}
